﻿using RuneMasterWin8.Entities;
using RuneMasterWin8.Entities.GameRecord;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;

namespace RuneMasterWin8.Util
{
    class LocalDataUtil
    {
        public static void SaveData(string key, object value)
        {
            ApplicationData.Current.LocalSettings.Values[key] = value;
        }

        public static object GetData(string key)
        {
            return ApplicationData.Current.LocalSettings.Values[key];
        }

        public static void RemoveData(string key)
        {
            ApplicationData.Current.LocalSettings.Values.Remove(key);
        }

        public static bool IsSetData(string key)
        {
            return ApplicationData.Current.LocalSettings.Values.ContainsKey(key);
        }

        static ObservableCollection<Type> _Types;

        public const string HIGHLIGHT = "HighLight.xml";
        public const string SAVED_GAME_RECORD = "SavedGameRecord.xml";
        public const string MODE_UNLOCK_STATUS = "ModeUnlockStatus.xml";
        public const string STAGE_UNLOCK_STATUS = "StageUnlockStatus.xml";

        public static async Task<object> XMLDeserialize(String fileName, Type type)
        {
            StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;

            StorageFile newFile = await folder.GetFileAsync(fileName);
            Stream newFileStream = await newFile.OpenStreamForReadAsync();
            //进行反序列化  
            DataContractSerializer ser = new DataContractSerializer(type, GetTypes());
            object instance = ser.ReadObject(newFileStream);
            newFileStream.Dispose();
            return instance;
        }

        public async static Task<bool> ExistFile(string fileName)
        {
            try
            {
                StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;
                StorageFile file = await folder.GetFileAsync(fileName);
                return true;
            }
            catch
            {
                return false;
            }

        }

        public async static Task XMLSerialize(object instance, string fileName, Type type)
        {
            //取得当前程序存放数据的目录  
            StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;
            //创建文件，如果文件存在就覆盖  
            StorageFile newFile = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
            //将内容序列化至文件  
            Stream newFileStream = await newFile.OpenStreamForWriteAsync();
            DataContractSerializer ser = new DataContractSerializer(type, GetTypes());
            ser.WriteObject(newFileStream, instance);
            newFileStream.Dispose();
        }

        private static ObservableCollection<Type> GetTypes()
        {
            //添加要序列化的类型  
            if (_Types == null)
            {
                _Types = new ObservableCollection<Type>();
                _Types.Add(typeof(SavedGameRecord));
                _Types.Add(typeof(SavedRune));
                _Types.Add(typeof(GameHighlights));
                _Types.Add(typeof(StringBoolDic));
            }
            return _Types;
        }


    }
}
